package com.los;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @program: java8
 * @description: stream特性之reduce
 * reduce 聚合函数
 * @author: Yujuan
 * @create: 2018-06-14 11:39
 **/
public class StreamReduce {
    public static void main(String[] args) {
        Stream<Integer> integerStream= Arrays.stream(new Integer[]{1,2,3,4,5,6,7,8,9});

        //加法
        Integer result=integerStream.reduce(0,(i,j)->i+j);
        System.out.println(result);
        //等价
        integerStream= Arrays.stream(new Integer[]{1,2,3,4,5,6,7,8,9});
        result=integerStream.reduce(0,Integer::sum);
        System.out.println(result);

        //最大值
        integerStream= Arrays.stream(new Integer[]{1,2,3,4,5,6,7,8,9});
        integerStream.reduce((i,j)->i>j?i:j).ifPresent(System.out::println);
        //等价
        integerStream= Arrays.stream(new Integer[]{1,2,3,4,5,6,7,8,9});
        integerStream.reduce(Integer::max).ifPresent(System.out::println);

        //最小值
        integerStream= Arrays.stream(new Integer[]{1,2,3,4,5,6,7,8,9});
        integerStream.reduce(Integer::min).ifPresent(System.out::println);

    }

}
